package com.ictech.print.web;

import android.webkit.JavascriptInterface;

import com.ictech.print.event.SwitchScreenEvent;
import com.ictech.print.job.PrintJob;
import com.ictech.print.utils.FileUtil;
import com.ictech.print.utils.HttpUtils;
import com.ictech.print.utils.NetworkUtil;
import com.ictech.print.utils.WifiConnector;

import de.greenrobot.event.EventBus;

public class AndroidtoJs {
	
	@JavascriptInterface
	public void setJobid(String jobid) {
		PrintJob.get().addJob(jobid);
	}
	
	@JavascriptInterface
	public String getEid() {
		return FileUtil.getEid();
	}
	
	@JavascriptInterface
	public void connectWifi(String wifiName, String wifiPass) {
		WifiConnector.get().connect(wifiName, wifiPass, WifiConnector.WifiCipherType.WIFICIPHER_WPA);
	}
	
	/**
	 * 判断是否连着eid的wifi
	 * @return 1 是连接eid的wifi，0 是连着普通的wifi，-1 是没连接wifi
	 */
	@JavascriptInterface
	public int isConnectEidWifi() {
		String wifiSSid = NetworkUtil.getWifiSsid();
		if ("未连接wifi".equals(wifiSSid)) {
			return -1;
		}
		String eid = FileUtil.getEid();
		if (eid != null && eid.equals(wifiSSid)) {
			return 1;
		}
		return 0;
	}
	
	@JavascriptInterface
	public String loadConfig() {
		return FileUtil.loadConfig();
	}
	
	@JavascriptInterface
	public String isWifiConnected() {
		return NetworkUtil.isWifiConnected() ? "1" : "0";
	}
	
	@JavascriptInterface
	public void closeWifi() {
		NetworkUtil.closeWifi();
	}
	
	@JavascriptInterface
	public String isWifiEnabled() {
		return NetworkUtil.isWifiEnabled() ? "1" : "0";
	}
	
	@JavascriptInterface
	public void switchScreen() {
		EventBus.getDefault().post(new SwitchScreenEvent());
	}
	
	@JavascriptInterface
	public String getWifiSsid() {
		return NetworkUtil.getWifiSsid();
	}
	
	@JavascriptInterface
	public String getGatewayIp() {
		return NetworkUtil.getGatewayIp();
	}
	
	@JavascriptInterface
	public void asynGet(final String url, final String callback, final String callbackParam) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpUtils.getInstance().asynGet(url, callback, callbackParam);
			}
			
		}).start();
	}
	
}
